package com.library.web.controller.app;

import com.library.common.annotation.Log;
import com.library.common.core.controller.BaseController;
import com.library.common.core.domain.AjaxResult;
import com.library.common.core.page.TableDataInfo;
import com.library.common.enums.BusinessType;
import com.library.common.utils.poi.ExcelUtil;
import com.library.domain.AppBanner;
import com.library.framework.manager.BizCodeManager;
import com.library.service.IAppBannerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @Author julyWhj
 * @Description $
 * @Date 2022/3/25 5:04 下午
 **/
@RestController
@RequestMapping("/app/banner")
public class AppBannerController extends BaseController {
    @Autowired
    private IAppBannerService appBannerService;
    @Autowired
    private BizCodeManager bizCodeManager;

    /**
     * 查询banner管理列表
     */
    @PreAuthorize("@ss.hasPermi('app:banner:list')")
    @GetMapping("/list")
    public TableDataInfo list(AppBanner appBanner) {
        startPage();
        List<AppBanner> list = appBannerService.selectAppBannerList(appBanner);
        return getDataTable(list);
    }

    /**
     * 导出banner管理列表
     */
    @PreAuthorize("@ss.hasPermi('app:banner:export')")
    @Log(title = "banner管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AppBanner appBanner) {
        List<AppBanner> list = appBannerService.selectAppBannerList(appBanner);
        ExcelUtil<AppBanner> util = new ExcelUtil<AppBanner>(AppBanner.class);
        util.exportExcel(response, list, "banner管理数据");
    }

    /**
     * 获取banner管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('app:banner:query')")
    @GetMapping(value = "/{bannerId}")
    public AjaxResult getInfo(@PathVariable("bannerId") Long bannerId) {
        return AjaxResult.success(appBannerService.selectAppBannerByBannerId(bannerId));
    }

    /**
     * 新增banner管理
     */
    @PreAuthorize("@ss.hasPermi('app:banner:add')")
    @Log(title = "banner管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AppBanner appBanner) {
        appBanner.setBannerNo(bizCodeManager.createBannerNo());
        return toAjax(appBannerService.insertAppBanner(appBanner));
    }

    /**
     * 修改banner管理
     */
    @PreAuthorize("@ss.hasPermi('app:banner:edit')")
    @Log(title = "banner管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AppBanner appBanner) {
        return toAjax(appBannerService.updateAppBanner(appBanner));
    }

    /**
     * 删除banner管理
     */
    @PreAuthorize("@ss.hasPermi('app:banner:remove')")
    @Log(title = "banner管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{bannerIds}")
    public AjaxResult remove(@PathVariable Long[] bannerIds) {
        return toAjax(appBannerService.deleteAppBannerByBannerIds(bannerIds));
    }
}
